package com.yantong.controller.api;

import com.yantong.annotation.Auth;
import com.yantong.controller.BaseController;
import com.yantong.pojo.MemberCart;
import com.yantong.pojo.PageDO;
import com.yantong.pojo.vo.CartDelallVo;
import com.yantong.pojo.vo.MemberCartVo;
import com.yantong.result.ResultVo;
import com.yantong.service.MemberCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: juzi
 * @date: 2021/10/25
 * @time: 16:12
 */
@ApiIgnore
@Controller
@RequestMapping("/wxapi/cart")
public class ApiCartController extends BaseController {



    @Autowired
    private MemberCartService cartService;


    /**
     * 添加购物车
     *
     * @author kanghaixin
     * @date 2018/9/7.15:39
     */
    @RequestMapping("/add")
    @ResponseBody
    @Auth
    public ResultVo addItemToCart(MemberCart memberCart) {
        int result = cartService.saveRedis(memberCart);

        if (result < 1) {
            return ResultVo.error();
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        return resultVo;
    }

    /**
     * 编辑购物车
     *
     * @author kanghaixin
     * @date 2018/9/7.15:39
     */
    @RequestMapping("/edit")
    @ResponseBody
    @Auth
    public ResultVo editCartItem(MemberCartVo memberCart) {
        int result = cartService.editCartRedis(memberCart);

        if (result < 1) {
            return ResultVo.error();
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        return resultVo;
    }

    /**
     * 删除购物车
     *
     * @author kanghaixin
     * @date 2018/9/7.15:39
     */
    @RequestMapping("/del")
    @ResponseBody
    @Auth
    public ResultVo delCartItem(Long memberId, Long centreId, String itemId, Integer isKuaidi) {
        cartService.removeItemRedis(memberId, centreId, itemId, isKuaidi);
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        return resultVo;
    }

    /**
     * 购物车商品总数量
     */
    @RequestMapping("/total")
    @ResponseBody
    @Auth(arg = 0)
    public ResultVo getCartTotal(Long memberId, Long centreId) {
        ResultVo resultVo = new ResultVo();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("mid", memberId);
        params.put("centreId", centreId);

        //int total = cartService.countTotal(params);
        int total = cartService.countTotalRedis(memberId, centreId);

        resultVo.put("data", total);
        resultVo.put("success", true);
        return resultVo;
    }

    /**
     * 清空购物车 @RequestParam("id[]") List<String > itemsId
     * Long memberId, Long centreId ,String itemsId,Integer isKuaidi
     */
    @RequestMapping("/delall")
    @ResponseBody
    @Auth
    public ResultVo delCartItems(@RequestBody CartDelallVo vo) {
        String[] itemsIds = vo.getId().split(",");
        List<String> list = Arrays.asList(itemsIds);
        for (String itemId : list) {
            cartService.removeItemRedis(vo.getMemberId(), vo.getCentreId(), itemId, vo.getIsKuaidi());
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        return resultVo;
    }

    /**
     * 购物车列表
     */
    @RequestMapping("/list")
    @ResponseBody
    //@Auth(arg = 0)
    public ResultVo getCartList(Long memberId, Long centreId, Long productId, Integer offset, Integer limit) {
        ResultVo resultVo = new ResultVo();
        if (memberId == null || centreId == null) {
            resultVo.put("success", true);
            resultVo.put("data", "");
            return resultVo;
        }
        PageDO<MemberCartVo> items = cartService.listMemberCarRedis(memberId, centreId, productId, offset, limit);
        resultVo.put("success", true);
        resultVo.put("data", items);
        return resultVo;
    }
}
